using System;
using TXK.CommonBase;

namespace TXK
{
    /// <summary>
    /// 数值转换类
    /// </summary>
    public class MigrationLib
    {
        private static string ByteMax = byte.MaxValue.ToString();

        private static string ByteMin = ((byte)0).ToString();

        private static string ShortMax = short.MaxValue.ToString();

        private static string ShortMin = short.MinValue.ToString();

        private static string UShortMax = ushort.MaxValue.ToString();

        private static string UShortMin = ((ushort)0).ToString();

        private static string IntMax = int.MaxValue.ToString();

        private static string IntMin = int.MinValue.ToString();

        private static string UIntMax = uint.MaxValue.ToString();

        private static string UIntMin = 0u.ToString();

        private static string GetErrorMsg(DataType type)
        {
            string empty = string.Empty;
            return type switch
            {
                DataType.Byte => "设置范围：" + ByteMin + "-" + ByteMax,
                DataType.Short => "设置范围：" + ShortMin + "-" + ShortMax,
                DataType.UShort => "设置范围：" + UShortMin + "-" + UShortMax,
                DataType.Int => "设置范围：" + IntMin + "-" + IntMax,
                DataType.UInt => "设置范围：" + UIntMin + "-" + UIntMax,
                _ => "设置范围超限",
            };
        }

        public static object GetMigrationValue(object value, string scale, string offset)
        {
            if (scale == "1" && offset == "0")
            {
                return value;
            }
            string name = value.GetType().Name;
            switch (name.ToLower())
            {
                case "byte":
                case "int16":
                case "uint16":
                case "int32":
                case "uint32":
                case "single":
                    return Convert.ToSingle((Convert.ToSingle(value) * Convert.ToSingle(scale) + Convert.ToSingle(offset)).ToString("N4"));
                case "int64":
                case "uint64":
                case "double":
                    return Convert.ToDouble((Convert.ToDouble(value) * Convert.ToDouble(scale) + Convert.ToDouble(offset)).ToString("N4"));
                default:
                    return value;
            }
        }

        public static Result<string> SetMigrationValue(string set, DataType type, string scale, string offset)
        {
            Result<string> xktResult = new Result<string>();
            if (scale == "1" && offset == "0")
            {
                try
                {
                    switch (type)
                    {
                        case DataType.Byte:
                            xktResult.Content = Convert.ToByte(set).ToString();
                            break;
                        case DataType.Short:
                            xktResult.Content = Convert.ToInt16(set).ToString();
                            break;
                        case DataType.UShort:
                            xktResult.Content = Convert.ToUInt16(set).ToString();
                            break;
                        case DataType.Int:
                            xktResult.Content = Convert.ToInt32(set).ToString();
                            break;
                        case DataType.UInt:
                            xktResult.Content = Convert.ToUInt32(set).ToString();
                            break;
                        case DataType.Long:
                            xktResult.Content = Convert.ToInt64(set).ToString();
                            break;
                        case DataType.ULong:
                            xktResult.Content = Convert.ToUInt64(set).ToString();
                            break;
                        case DataType.Float:
                            xktResult.Content = Convert.ToSingle(set).ToString();
                            break;
                        case DataType.Double:
                            xktResult.Content = Convert.ToDouble(set).ToString();
                            break;
                        default:
                            xktResult.Content = set;
                            break;
                    }
                    xktResult.IsSuccess = true;
                    return xktResult;
                }
                catch (Exception)
                {
                    xktResult.IsSuccess = false;
                    xktResult.Message = "转换出错，" + GetErrorMsg(type);
                    return xktResult;
                }
            }
            try
            {
                switch (type)
                {
                    case DataType.Byte:
                        xktResult.Content = Convert.ToByte((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case DataType.Short:
                        xktResult.Content = Convert.ToInt16((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case DataType.UShort:
                        xktResult.Content = Convert.ToUInt16((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case DataType.Int:
                        xktResult.Content = Convert.ToInt32((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case DataType.UInt:
                        xktResult.Content = Convert.ToUInt32((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case DataType.Long:
                        xktResult.Content = Convert.ToInt64((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
                        break;
                    case DataType.ULong:
                        xktResult.Content = Convert.ToUInt64((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
                        break;
                    case DataType.Float:
                        xktResult.Content = Convert.ToSingle((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case DataType.Double:
                        xktResult.Content = Convert.ToDouble((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
                        break;
                    default:
                        xktResult.Content = set;
                        break;
                }
                xktResult.IsSuccess = true;
                return xktResult;
            }
            catch (Exception)
            {
                xktResult.IsSuccess = false;
                xktResult.Message = "转换出错，" + GetErrorMsg(type);
                return xktResult;
            }
        }

        public static Result<string> SetMigrationValue(string set, ComplexDataType type, string scale, string offset)
        {
            Result<string> xktResult = new Result<string>();
            if (scale == "1" && offset == "0")
            {
                try
                {
                    switch (type)
                    {
                        case ComplexDataType.Byte:
                            xktResult.Content = Convert.ToByte(set).ToString();
                            break;
                        case ComplexDataType.Short:
                            xktResult.Content = Convert.ToInt16(set).ToString();
                            break;
                        case ComplexDataType.UShort:
                            xktResult.Content = Convert.ToUInt16(set).ToString();
                            break;
                        case ComplexDataType.Int:
                            xktResult.Content = Convert.ToInt32(set).ToString();
                            break;
                        case ComplexDataType.UInt:
                            xktResult.Content = Convert.ToUInt32(set).ToString();
                            break;
                        case ComplexDataType.Long:
                            xktResult.Content = Convert.ToInt64(set).ToString();
                            break;
                        case ComplexDataType.ULong:
                            xktResult.Content = Convert.ToUInt64(set).ToString();
                            break;
                        case ComplexDataType.Float:
                            xktResult.Content = Convert.ToSingle(set).ToString();
                            break;
                        case ComplexDataType.Double:
                            xktResult.Content = Convert.ToDouble(set).ToString();
                            break;
                        default:
                            xktResult.Content = set;
                            break;
                    }
                    xktResult.IsSuccess = true;
                    return xktResult;
                }
                catch (Exception)
                {
                    xktResult.IsSuccess = false;
                    xktResult.Message = "转换出错";
                    return xktResult;
                }
            }
            try
            {
                switch (type)
                {
                    case ComplexDataType.Byte:
                        xktResult.Content = Convert.ToByte((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case ComplexDataType.Short:
                        xktResult.Content = Convert.ToInt16((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case ComplexDataType.UShort:
                        xktResult.Content = Convert.ToUInt16((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case ComplexDataType.Int:
                        xktResult.Content = Convert.ToInt32((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case ComplexDataType.UInt:
                        xktResult.Content = Convert.ToUInt32((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case ComplexDataType.Long:
                        xktResult.Content = Convert.ToInt64((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
                        break;
                    case ComplexDataType.ULong:
                        xktResult.Content = Convert.ToUInt64((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
                        break;
                    case ComplexDataType.Float:
                        xktResult.Content = Convert.ToSingle((Convert.ToSingle(set) - Convert.ToSingle(offset)) / Convert.ToSingle(scale)).ToString();
                        break;
                    case ComplexDataType.Double:
                        xktResult.Content = Convert.ToDouble((Convert.ToDouble(set) - Convert.ToDouble(offset)) / Convert.ToDouble(scale)).ToString();
                        break;
                    default:
                        xktResult.Content = set;
                        break;
                }
                xktResult.IsSuccess = true;
                return xktResult;
            }
            catch (Exception)
            {
                xktResult.IsSuccess = false;
                xktResult.Message = "转换出错";
                return xktResult;
            }
        }
    }
}
